﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class InstallmentWarningListViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _loanRepository;
        /// <summary>
        /// 
        /// </summary>
        IImageRepository _ImageRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="InstallmentWarningListViewModel"/> class.
        /// </summary>
        /// <param name="loanRepository">The loan repository.</param>
        /// <param name="imageRepository">The image repository.</param>
        public InstallmentWarningListViewModel(ILoanRepository loanRepository, IImageRepository imageRepository)
        {
            _loanRepository = loanRepository;
            _ImageRepository = imageRepository;
        }

        /// <summary>
        /// Gets the loans warnings.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetLoansWarnings()
        {
            return from warning in _loanRepository.FindLoanInstallmentWarnings(instWarning => true)
                   select new
                   {
                       LoanIntstallmentWarningID = warning.LoanIntstallmentWarningID,
                       LoanNumber = warning.LoanExecutionInstallment.LoanExecution.CustomerLoan.LoanNumber,
                       CustomerName = warning.LoanExecutionInstallment.LoanExecution.CustomerLoan.CustomerObj.CustomerName,
                       LoanDate = warning.LoanExecutionInstallment.LoanExecution.CustomerLoan.LoanCreatedDate,
                       WarningDate = warning.WarningDate
                   };
        }

        /// <summary>
        /// Saves the installment warning attachment.
        /// </summary>
        /// <param name="installmentWarningID">The installment warning ID.</param>
        /// <param name="insertedAttachmentsIDs">The inserted attachments I ds.</param>
        /// <param name="deletedAttachmentIDs">The deleted attachment I ds.</param>
        public void SaveInstallmentWarningAttachment(Guid installmentWarningID, IEnumerable<Guid> insertedAttachmentsIDs, IEnumerable<Guid> deletedAttachmentIDs)
        {
            using (IUnitOfWork trans = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                //var instWarningOldAttchmentsIDs = _loanRepository.GetLoanInstallmentWarning(installmentWarningID).LoanInstallmentWarningAttachments.Select(
                //    att => att.LoanInstallmentWarningAttachmentID);

                //_loanRepository.DeleteLoanInstallmentWarningAttachment(
                //    att => instWarningOldAttchmentsIDs.Contains(att.LoanInstallmentWarningAttachmentID));


                foreach (var insertedID in insertedAttachmentsIDs)
                {
                    if (!(_loanRepository.FindLoanInstallmentWarningAttachment(
                        att => att.LoanIntallmentWarningID.Equals(installmentWarningID) &&
                            att.Attachment.Equals(insertedID)).Count() > 0))
                    {
                        ILoanInstallmentWarningAttachment warningAttachment = ServiceLocator.Current.GetInstance<ILoanInstallmentWarningAttachment>();
                        warningAttachment.LoanInstallmentWarningAttachmentID = Guid.NewGuid();
                        warningAttachment.LoanIntallmentWarningID = installmentWarningID;
                        warningAttachment.Attachment = insertedID;
                        _loanRepository.CreateLoanInstallmentWarningAttachment(warningAttachment);
                    }
                }
                foreach (var deletedID in deletedAttachmentIDs)
                {
                    var ssd = _loanRepository.FindLoanInstallmentWarningAttachment(warningAttach => warningAttach.LoanIntallmentWarningID == installmentWarningID && warningAttach.Attachment == deletedID);
                    _loanRepository.DeleteLoanInstallmentWarningAttachment(warningAttach => warningAttach.LoanIntallmentWarningID == installmentWarningID && warningAttach.Attachment == deletedID);
                }
                trans.Commit();
            }

        }

        /// <summary>
        /// Gets the warning attachment.
        /// </summary>
        /// <param name="installmentWarningID">The installment warning ID.</param>
        /// <returns></returns>
        public IEnumerable<IImage> GetWarningAttachment(Guid installmentWarningID)
        {
            var attchmentIDs = _loanRepository.GetLoanInstallmentWarning(installmentWarningID).LoanInstallmentWarningAttachments.Select(
                attachment => attachment.Attachment);
            return _ImageRepository.FindImage(image => attchmentIDs.Contains(image.ImageID));
        }
    }
}
